surface.CreateFont( "verdana", 17, 400, true, false, "TeammodDerma" )

local texGradient = surface.GetTextureID( "gui/center_gradient" )
local shield = surface.GetTextureID( "gui/silkicons/shield" )
local open = surface.GetTextureID( "gui/silkicons/heart" )

local function timerfunc( panel )
    if panel.List then
        panel:AddTeams()
        panel.List:Rebuild()
    else
        timer.Destroy( panel )
    end
end

local PANEL = {}
AccessorFunc( PANEL , "color" , "Color" )

function PANEL:Init()
    local avatar = vgui.Create( "AvatarImage" , self)
    self.avatar = avatar
end

function PANEL:Invalidate()
    self.avatar:SetSize(32 , 32)
    self.avatar:SetPos( 2 , 2)

    self:SetTall(36)

end

function PANEL:Paint()
    draw.RoundedBox(4 , 0 , 0 ,self:GetWide() , self:GetTall() , self.color)
    surface.SetTexture( texGradient )
    surface.SetDrawColor( 255, 255, 255, 150 )
    surface.DrawTexturedRect( 0, 0, self:GetWide() , self:GetTall() ) 

    draw.DrawText( self.ply:Nick() , "TeammodDerma" , 38 , 10 , Color( 0 , 0 , 0 , 255 ) , TEXT_ALIGN_LEFT )

    draw.DrawText( self.ply:Frags() , "TeammodDerma" , 691 , 10 , Color( 0 , 0 , 0 , 255 ) , TEXT_ALIGN_LEFT )

    draw.DrawText( self.ply:Deaths() , "TeammodDerma" , 791 , 10 , Color( 0 , 0 , 0 , 255 ) , TEXT_ALIGN_LEFT )
end

function PANEL:SetPlayer( ply )
    if self.ply then return end
    self.ply = ply
    self.avatar:SetPlayer( ply )
end

function PANEL:PerformLayout()
    //self.BaseClass:PerformLayout( self )

    self:Invalidate()
end
vgui.Register( "PlayerRow" , PANEL , "DPanel")

local PANEL = {}
AccessorFunc( PANEL , "team" , "Team" )

function PANEL:Init()
    local List = vgui.Create("DPanelList" , self )
    List:SetPadding( 2 )
    List:SetSpacing( 2 )

    local but = vgui.Create( "DButton" , self )
    self.but = but

    //List:EnableVerticalScrollbar( true )

    self.List = List
    self.color = Color(255 , 255 ,255 ,40)

    self:SetLabel( "" )
    self:SetExpanded( 0 )
    self:SetContents( List )

end

function PANEL:Invalidate()
    local w,h,padding = self:GetWide() , self:GetTall() , 18

    self.List:SetTall(200)
    self.List:SetAutoSize( true )
    self.Header:SetTall(40)

    self.but:SetSize( 100 , 30 )
    self.but:SetPos(850 , 5 )
    self.but:SetText( "" )
    //MsgN( self.but.Base )
end

function PANEL:AddPlayers()
    if self.team.Password != "" then
        self.passlogo = shield
        self.protected = "(Passworded)"
    else
        self.passlogo = open
        self.protected = "(Open)"
    end
    self.List:Clear()
    self.numplayers = 0
    self.totalkills = 0
    self.totaldeaths = 0 
    local players = self.team.players
    for k , v in pairs(players) do
        if v:IsValid() then
            self.numplayers = self.numplayers + 1
            self.totalkills = self.totalkills + v:Frags()
            self.totaldeaths = self.totaldeaths + v:Deaths()
            local plypnl = vgui.Create( "PlayerRow" )
            plypnl:SetPlayer( v )
            plypnl:SetColor( self.team.color)
            self.List:AddItem( plypnl )
        end
    end
end

function PANEL:Paint()
    draw.RoundedBox( 4, 0, 0, self:GetWide() , 40 , self.team.color )

    surface.SetTexture( texGradient )
    surface.SetDrawColor( 255, 255, 255, 150 )
    surface.DrawTexturedRect( 0, 0, self:GetWide() , 40  )

    surface.SetTexture( self.passlogo )
    surface.SetDrawColor( 255, 255, 255, 150 )
    surface.DrawTexturedRect( 450 , 6, 16 , 16  ) 
    draw.DrawText( self.protected , "Default" , 447 , 22 , Color( 0 , 0 , 0 , 255 ) , TEXT_ALIGN_LEFT )

    draw.DrawText( self.team.name , "TeammodDerma" , 20 , 10 , Color( 0 , 0 , 0 , 255 ) , TEXT_ALIGN_LEFT )

    draw.DrawText( self.team.CreatorName  , "TeammodDerma" , 300 , 10 , Color( 0 , 0 , 0 , 255 ) , TEXT_ALIGN_LEFT )

    draw.DrawText( self.numplayers , "TeammodDerma" , 593 , 10 , Color( 0 , 0 , 0 , 255 ) , TEXT_ALIGN_LEFT )

    draw.DrawText( self.totalkills , "TeammodDerma" , 693 , 10 , Color( 0 , 0 , 0 , 255 ) , TEXT_ALIGN_LEFT )

    draw.DrawText( self.totaldeaths , "TeammodDerma" , 793 , 10 , Color( 0 , 0 , 0 , 255 ) , TEXT_ALIGN_LEFT )

end

function PANEL:SetupEvents()
    local parent = self
    
    self.Header.OnMousePressed = function(self , mcode )

        if !(parent.numplayers > 0 ) then return end
        if ( mcode == MOUSE_LEFT ) then
            parent:Toggle()
            return 
        end
        
        return parent:OnMousePressed( mcode )
    end

    function self.but:Paint()
        draw.RoundedBox( 4 , 0 , 0 , self:GetWide() , self:GetTall() , Color( 255 ,255 , 255 , 70 )  )

        draw.DrawText( "Join Team" , "TeammodDerma" , 15 , 5 , Color( 50 , 50 , 50 , 255 ) , TEXT_ALIGN_LEFT )
    end

    local function passcheck( text )
        if util.CRC( text ) != parent.team.Password then
            Derma_Message( "Wrong Password", "Wrong Password" , "Close" )
        else
            RunConsoleCommand( "teammod_join" , tostring( parent.team.number ) , text )
        end
    end

    function self.but:DoClick()
        if parent.team.Password == "" then
            RunConsoleCommand("teammod_join" , tostring(parent.team.number ) )
        else
            Derma_StringRequest( "Enter a Password", "Password" , "" , passcheck )
        end
    end
end

function PANEL:PerformLayout()
    self.BaseClass.PerformLayout(self);

    self:Invalidate();
    self:SetupEvents()
end
vgui.Register( "SingleTeam" , PANEL , "DCollapsibleCategory")



local PANEL = {}

function PANEL:Init()

    if not TeamMod.Teams then return end

    local teamcol = vgui.Create( "Label" , self )
    self.teamcol = teamcol

    local owner = vgui.Create( "Label" , self)
    self.ownerpnl = owner

    local players = vgui.Create( "Label" , self)
    self.ply = players

    local kills = vgui.Create( "Label" , self)
    self.kill = kills

    local deaths = vgui.Create( "Label" , self)
    self.deaths = deaths

    local pass = vgui.Create( "Label" , self)
    self.pass = pass   

    local join = vgui.Create( "Label" , self)
    self.join = join

    local List = vgui.Create( "DPanelList" , self )
    List:SetPadding( 6 )
    List:SetSpacing( 6 )
    List:EnableVerticalScrollbar( true )
    self.List = List


    self:SetupEvents();
    self:AddTeams()
end

function PANEL:AddTeams()
    local items = self.List:GetItems()
    for i = 1 , #TeamMod.Teams do
        if !TeamMod.Teams[i] then return end
        local tTeam = TeamMod.Teams[i]
        local panel = items[i]

        if panel then 
            panel:SetTeam( tTeam )
            panel:AddPlayers()
        else
            panel = vgui.Create("SingleTeam" , self )
            panel:SetTeam( tTeam )
            panel:AddPlayers()
            self.List:AddItem( panel )
        end
    end
    timer.Create( self , 0.5 , 0 , timerfunc , self )
end

function PANEL:Invalidate()
    local w,h,padding = self:GetWide() , self:GetTall() , 5
    
    self.teamcol:SetSize(100 , 20)
    self.teamcol:SetPos( 30 , 8)
    self.teamcol:SetText( "Team" )

    self.ownerpnl:SetSize(100 , 20)
    self.ownerpnl:SetPos(312, 8)
    self.ownerpnl:SetText( "Owner" )

    self.ply:SetSize(100 , 20)
    self.ply:SetPos(595 , 8)
    self.ply:SetText( "Players" )

    self.kill:SetSize(100 , 20)
    self.kill:SetPos( 700 , 8)
    self.kill:SetText("Kills")

    self.deaths:SetSize(100 , 20)
    self.deaths:SetPos( 793 , 8)
    self.deaths:SetText("Deaths")

    self.pass:SetSize(100 , 20)
    self.pass:SetPos( 444 , 8)
    self.pass:SetText("Passworded")

    self.join:SetSize(100 , 20)
    self.join:SetPos( 886 , 8)
    self.join:SetText("Join Team")

    self.List:SetSize( w - padding * 2 , h - padding * 2 - 25 )
    self.List:SetPos( padding  , padding + 25)
end

function PANEL:ApplySchemeSettings()

    self.teamcol:SetFont("Default")
    self.teamcol:SetFGColor( Color( 0 , 0 , 0 , 255 ))

    self.ownerpnl:SetFont("Default")
    self.ownerpnl:SetFGColor( Color( 0 , 0 , 0 , 255 ))

    self.ply:SetFont("Default")
    self.ply:SetFGColor( Color( 0 , 0 , 0 , 255 ))

    self.kill:SetFont("Default")
    self.kill:SetFGColor( Color( 0 , 0 , 0 , 255 ))

    self.deaths:SetFont("Default")
    self.deaths:SetFGColor( Color( 0 , 0 , 0 , 255 ))

    self.pass:SetFont("Default")
    self.pass:SetFGColor( Color( 0 , 0 , 0 , 255 ))

    self.join:SetFont("Default")
    self.join:SetFGColor( Color( 0 , 0 , 0 , 255 ))
end

function PANEL:SetupEvents()
    
end

function PANEL:PerformLayout()
    self.BaseClass.PerformLayout(self);

    self:Invalidate();
end

vgui.Register("TeamSelect",PANEL,"DPanel")



local PANEL = {}

function PANEL:Init()
    local circ = vgui.Create( "DColorCircle" , self)
    circ:SetRGB( Color( 255 , 255 , 255 , 255 ) )
    self.circ = circ

    local disp = vgui.Create( "Panel" , self)
    self.disp = disp

    local alph = vgui.Create( "DRGBBar" , self )
    alph:SetBackground("vgui/hsv-brightness")
    alph:SetSlideY( 0.01 )
    self.alph = alph

    local List = vgui.Create("DPanelList" , self )
    List:SetSpacing( 48 )
    List:SetDrawBackground( false )
    self.List = List

    local teamname = vgui.Create( "Label" , self )
    self.teamname = teamname

    local name = vgui.Create( "DTextEntry" , self )
    self.namepnl = name
    List:AddItem(name)

    local passdisp = vgui.Create( "Label" , self)
    self.passdisp = passdisp

    local protected = vgui.Create( "DTextEntry" , self)
    self.prot = protected
    List:AddItem( protected )

    local create = vgui.Create( "DButton" , self )
    self.create = create
    List:AddItem( create )

    local teamcol = vgui.Create( "Label" , self )
    self.teamcol = teamcol

    local owner = vgui.Create( "Label" , self)
    self.ownerpnl = owner

    local kills = vgui.Create( "Label" , self)
    self.kill = kills

    local deaths = vgui.Create( "Label" , self)
    self.deaths = deaths

    local pass = vgui.Create( "Label" , self)
    self.pass = pass   


    self.mockup = {
        name = "Default",
        color = Color( 255 , 255 , 255 , 255),
        passlogo = open,
        protected = "(Open)",
        password = ""
    }

    self:SetupEvents()
end

function PANEL:Invalidate()
    local w,h,padding = self:GetWide() , self:GetTall() , 12
    
    self.circ:SetSize( 330 , 330 )
    self.circ:SetPos( padding / 2 , padding / 2 )

    self.disp:SetSize( w - padding , 48)
    self.disp:SetTall( 48 )
    self.disp:AlignLeft( padding / 2)
    self.disp:AlignBottom( padding /2)

    self.alph:SetSize(50 , 330)
    self.alph:SetPos( 330 + padding * 2 + 25 , padding / 2)

    self.teamname:SetText( "Enter a Team name (30 characters max)")
    self.teamname:SetFont("Default")
    self.teamname:SetFGColor( Color(0,0,0,255) )
    self.teamname:SetSize(200 , 25)
    self.teamname:AlignTop( padding + 9 )
    self.teamname:AlignRight( 272 )

    self.List:SetSize(400 , 300)
    self.List:AlignTop( padding + 30)
    self.List:AlignRight( 73 )

    self.passdisp:SetText( "Enter a Password")
    self.passdisp:SetFont("Default")
    self.passdisp:SetFGColor( Color(0,0,0,255) )
    self.passdisp:SetSize(200 , 25)
    self.passdisp:AlignTop( padding + 77 )
    self.passdisp:AlignRight( 272 )

    self.teamcol:SetSize(100 , 20)
    self.teamcol:SetPos( 27 , 360)
    self.teamcol:SetText( "Team" )

    self.ownerpnl:SetSize(100 , 20)
    self.ownerpnl:SetPos(309, 360)
    self.ownerpnl:SetText( "Owner" )

    self.create:SetTall( 50 )
    self.create:SetText( "Create Team")

    self.kill:SetSize(100 , 20)
    self.kill:SetPos( 697 , 360)
    self.kill:SetText("Kills")

    self.deaths:SetSize(100 , 20)
    self.deaths:SetPos( 790 , 360)
    self.deaths:SetText("Deaths")

    self.pass:SetSize(100 , 20)
    self.pass:SetPos( 441 , 360)
    self.pass:SetText("Passworded")

    self.teamcol:SetFont("Default")
    self.teamcol:SetFGColor( Color( 0 , 0 , 0 , 255 ))

    self.ownerpnl:SetFont("Default")
    self.ownerpnl:SetFGColor( Color( 0 , 0 , 0 , 255 ))

    self.kill:SetFont("Default")
    self.kill:SetFGColor( Color( 0 , 0 , 0 , 255 ))

    self.deaths:SetFont("Default")
    self.deaths:SetFGColor( Color( 0 , 0 , 0 , 255 ))

    self.pass:SetFont("Default")
    self.pass:SetFGColor( Color( 0 , 0 , 0 , 255 ))


end

function PANEL:SetupEvents()
    local parent = self
    parent.circ.val = 0

    function self.create:DoClick()
        if !parent.mockup.color.r || !parent.mockup.color.g || !parent.mockup.color.b || !parent.mockup.name then return end
        local pass
        if parent.mockup.password == "" then pass = nil else pass = parent.mockup.password end
        
        RunConsoleCommand( "teammod_create_and_join" , parent.mockup.name , parent.mockup.color.r , parent.mockup.color.g , parent.mockup.color.b , pass )
    end

    function self.prot:OnTextChanged()
        if self:GetText() != "" then
            parent.mockup.passlogo = shield
            parent.mockup.password = self:GetText()
            parent.mockup.protected = "(Passworded)"
        else 
            parent.mockup.passlogo = open
            parent.mockup.protected = "(Open)"
        end
    end
    function self.namepnl:OnTextChanged()
        if #self:GetText() > 30 then
            self:SetText( string.Left( self:GetText() , 30 ) )
            self:SetCaretPos( 30 )
        end 
        parent.mockup.name = self:GetText()
    end

    function self.alph:TranslateValues( x , y )
        local h , s , v = ColorToHSV( parent.circ:GetRGB() )

        local val = math.abs( y - 1 )
        local col = HSVToColor( h , s , val )
        parent.mockup.color = col
        parent.circ.val = y

        y = math.Clamp( y , 0.01 , 0.99 )

        return x , y
    end

    function self.circ:OnCursorMoved( x , y )

        if ( !self.Dragging ) then return end
        
        local w, h = self:GetSize()
        local iw, ih = self.Knob:GetSize()

        
        if ( self.m_bTrappedInside ) then
        
            w = w - iw
            h = h - ih
            
            x = x - iw * 0.5
            y = y - ih * 0.5
        end
        
        x = math.Clamp( x, 0, w ) / w
        y = math.Clamp( y, 0, h ) / h
        
        if ( self.m_iLockX ) then x = self.m_iLockX end
        if ( self.m_iLockY ) then y = self.m_iLockY end
        
        local x , y = self:TranslateValues( x, y )

        self:SetSlideX( x )
        self:SetSlideY( y )

        parent.mockup.color = self:GetRGB()
        parent.alph:TranslateValues( 0 , self.val )
    end

    function self.disp.PaintOver()
        draw.RoundedBox( 4, 0, 0, self:GetWide() - 12 , 48, self.mockup.color )

        surface.SetTexture( texGradient )
        surface.SetDrawColor( 255, 255, 255, 150 )
        surface.DrawTexturedRect( 0, 0, self:GetWide() , 48  )

        surface.SetTexture( self.mockup.passlogo )
        surface.SetDrawColor( 255, 255, 255, 150 )
        surface.DrawTexturedRect( 450 , 7, 16 , 16  ) 
        draw.DrawText( self.mockup.protected , "Default" , 447 , 23 , Color( 0 , 0 , 0 , 255 ) , TEXT_ALIGN_LEFT )

        draw.DrawText( self.mockup.name, "TeammodDerma" , 20 , 15 , Color( 0 , 0 , 0 , 255 ) , TEXT_ALIGN_LEFT )

        draw.DrawText( LocalPlayer():Nick()  , "TeammodDerma" , 300 , 15 , Color( 0 , 0 , 0 , 255 ) , TEXT_ALIGN_LEFT )

        draw.DrawText( LocalPlayer():Frags() , "TeammodDerma" , 693 , 15 , Color( 0 , 0 , 0 , 255 ) , TEXT_ALIGN_LEFT )

        draw.DrawText( LocalPlayer():Deaths() , "TeammodDerma" , 793 , 15 , Color( 0 , 0 , 0 , 255 ) , TEXT_ALIGN_LEFT )

    end

end

function PANEL:PerformLayout()
    self.BaseClass.PerformLayout(self);

    self:Invalidate();
end

vgui.Register("TeamModCreatorPanel",PANEL,"DPanel")



local PANEL = {}

function PANEL:Init()

    if not TeamMod.Teams then return end
    local sheet = vgui.Create( "DPropertySheet" , self )
    self.sheet = sheet
    self.sheet.tabScroller:SetOverlap( 0 )

    local teamjoin = vgui.Create( "TeamSelect" , self )
    self.teamjoin = teamjoin
    sheet:AddSheet( "Join Teams" , teamjoin , "gui/silkicons/user", false, false, "Choose a team to join here")

    local teamcreate = vgui.Create( "TeamModCreatorPanel" , self )
    self.teamcreate = teamcreate
    local test = sheet:AddSheet( "Create Teams" , teamcreate , "gui/silkicons/add" , false , false , "Create A Team")

    self:SetupEvents();
    self:MakePopup()
    self:SetDraggable(false)
    self:SetBackgroundBlur(true)
    self:SetTitle("Create and Join Teams")
    self:Center()
end



function PANEL:Invalidate()
    local w,h,padding = self:GetWide() , self:GetTall() , 12
    
    self:SetSize( 1000 , 500)

    self.sheet:SetSize( w - padding , h - padding - 22 )
    self.sheet:SetPos( padding /2 , padding /2 + 22 )



end

function PANEL:SetupEvents()
    
end

function PANEL:PerformLayout()
    self.BaseClass.PerformLayout(self);

    self:Invalidate();
end

vgui.Register("TeamModBasePanel",PANEL,"DFrame")


local function OpenMenu(um)
    vgui.Create("TeamModBasePanel")
end
usermessage.Hook("teammod_openmenu" , OpenMenu )

